<template>
	<view>
		<view>
			<label v-for="(item,index) in TableList" style="padding-left: 5px;">
				{{item.label}}
			</label>
		</view>
		<checkbox-group @change="checkboxChange">
			<view v-for="(item,index) in datalist">
				<label>
					<view>
						<checkbox :value="item.ID" :checked="Pagination.Ids.some(it=>it==item.ID)" />
					</view>
					<view @click="schoolEdit(item.ID)">
						<label v-for="(tl,index) in TableList">
							{{item[tl.name[1]]}}
						</label>
						<button style="float: right;" type="default" size="mini"
							@tap.stop="SchoolDel(item.ID,index)">删除</button>
					</view>
				</label>
			</view>
		</checkbox-group>
	</view>
</template>

<script>
	import {
		Reset
	} from '../../pages/schoollist/controller/index.js'
	import ajax from '../../myservice/ajaxservice.js'

	export default {
		data() {
			return {
				datalist: [],
				//Ids: this.Pagination.Ids
			}
		},
		props: {
			// 检测类型 + 其他验证
			Entity: {
				type: Array,
				default: () => {
					return [];
				},
				required: true,
			},
			TableList: {
				type: Array,
				default: () => {
					return []
				},
				required: true
			}
		},
		inject: ["Pagination"],
		watch: {
			'Pagination.Page': function(val, oldVal) {
				console.log(JSON.stringify(val) + "___" + JSON.stringify(oldVal))
				if (val == 0) {
					this.datalist = [];
					this.Pagination.Page = 1;
					this.Pagination.Ids=[];
					this.GetSearch();
				} else if (val > 1) {
					this.GetSearch();
				}
			},
			'Pagination.Deletes': function(val, oldVal) {
				if (val) {
					this.SchoolDelete();
					this.Pagination.Deletes = false;
				}
			}
		},
		methods: {
			GetSearch() {
				var that = this;
				var data = that.Pagination;
				this.Entity.map(item => {
					data[item.name[1]] = item.value;
				})
				ajax.webRequest({
					url: Reset.search,
					method: 'POST',
					data: data
				}).then(res => {
					console.log(res);
					//that.datalist = res.data.Data;
					if (res.data.Data.length > 0) {
						that.datalist = that.datalist.concat(res.data.Data);
					}
				})
			},
			SchoolDelete() { //批量删除
				var that = this;
				if (this.Pagination.Ids== null || that.Pagination.Ids.length <= 0) {
					uni.showToast({
						title: '没有选中任何项',
						icon: 'none'
					});
					return;
				}
				uni.showModal({
					title: '提示',
					content: '是否确认删除选中信息',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							ajax.webRequest({
								url: Reset.delete,
								method: 'POST',
								data: that.Pagination.Ids
							}).then(res => {
								console.log(JSON.stringify(res));
								if (res.statusCode == 200) {
									uni.showToast({
										title: '删除成功'
									});
									that.datalist = that.datalist.filter(function(currentValue,
										index) {
										return !that.Pagination.Ids.some(item => item == currentValue.ID);
									})
								}
							})
						} else {
							uni.showToast({
								title: '取消'
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			schoolEdit() {

			},
			checkboxChange(e) {
				this.Pagination.Ids = e.detail.value;
			},
			SchoolDel: function(ID, index) { //删除学校信息
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该信息',
					showCancel: true,
					success: res => {
						if (res.confirm) {
							ajax.webRequest({
								url: Reset.delete,
								method: 'POST',
								data: [ID]
							}).then(res => {
								console.log(JSON.stringify(res));
								if (res.statusCode == 200) {
									uni.showToast({
										title: '删除成功'
									});
									// 从索引处删除一个元素
									that.datalist.splice(index, 1);
									//var arr = Array.from(that.dataSchoolList);
									//arr.splice(index,1);
								}
							})
						} else {
							uni.showToast({
								title: '取消'
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
		}
	}
</script>

<style>
</style>
